<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
			<el-row>
				<el-col :span="24">
					<el-form-item label="关联部门" prop="deptcode"  :rules="rules.validateRequire">
						<tl-ref-dept v-model="model.deptcode" :initDisplayData.sync="model.dispdeptcode" dataType="code" nameType="fullname" :singleSelect="false"></tl-ref-dept>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="部门通称" prop="deptcommoncode" :rules="rules.validateRequire">
						<el-select v-model="model.deptcommoncode" filterable clearable>
							<el-option v-for="item in codeModel.deptcommoncodeList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="部门代码" prop="deptcommonmark" :rules="rules.validateRequire">
						<el-select v-model="model.deptcommonmark" filterable clearable>
							<el-option v-for="item in codeModel.deptcommonmarkList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="省总" prop="provincemanagercode"  :rules="rules.validateRequire">
						<tl-ref-user v-model="model.provincemanagercode" :initDisplayData.sync="model.provincemanagername" dataType="code" :singleSelect="true"></tl-ref-user>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="财经副总" prop="cfocode"  :rules="rules.validateRequire">
						<tl-ref-user v-model="model.cfocode" :initDisplayData.sync="model.cfoname" dataType="code" :singleSelect="true"></tl-ref-user>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="部门CHO" prop="provincechocode"  :rules="rules.validateRequire">
						<tl-ref-user v-model="model.provincechocode" :initDisplayData.sync="model.provincechoname" dataType="code" :singleSelect="true"></tl-ref-user>
					</el-form-item>
				</el-col>
        <el-col :span="12">
          <el-form-item label="ERP地区明细编码" prop="erpregioncode">
            <el-input v-model="model.erpregioncode"></el-input>
          </el-form-item>
        </el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="排序码" prop="sortcode">
						<el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="启用" prop="enabled">
						<el-radio-group v-model="model.enabled">
							<el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="备注" prop="description" >
						<el-input v-model="model.description" type="textarea" :rows="4"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { },
  data() {
    return {
      moduleTitle: '部门扩展信息',
      urlPrefix: '/mms/deptExtraInfo/',
      url: {
        // 额外的url
		// 判断是否为一级部门
		checkFirstlevelDept: 'mms/deptExtraInfo/checkFirstlevelDept'
      }
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
			if (!this.isAdd) {
				this.model.dispdeptcode = this.model.deptname
			}
    },
	// 保存前，判断所选关联部门是否为一级部门
	async doSubmitEdit() {
      if (!await this.beforeSubmit()) {
        return
      }
      // 未填写字段赋值
      this.$set(this.model, 'deptname', this.model.dispdeptcode)
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        } else {
          return false
        }
      })
    },
	async beforeSubmit() {
		return await getAction(this.url.checkFirstlevelDept, { deptcode: this.model.deptcode }).then((res) => {
        if (res.result.key === '1') {
            return true
          } else {
            this.$tap.alert('关联部门只能选择一级部门！')
            return false
          }
        })
	}
  }
}
</script>

